<script setup>
import api from '@/api/index.js'
import { message } from 'ant-design-vue'
import { computed, ref, h, provide } from 'vue'
import MyTable from '@/components/MyTable/MyTable.vue'
import { Modal } from 'ant-design-vue'
import ImageView from '@/components/ImageView/ImageView.vue'
import { getConsants } from '@/utils/index.js'

const props = defineProps({
  // 表格数据总条数
  total: { type: Number, default: 0 },
  // 表格当前页码
  pageNum: { type: Number, default: 1 }
})
// 分页参数
const pagination = computed(() => ({
  total: props.total,
  showSizeChanger: false,
  current: props.pageNum,
  onChange: (e) => {
    emits('pageChange', e)
  }
}))

const emits = defineEmits(['edit', 'pageChange', 'reload'])

// 表格列配置
const columns = ref([
  { ellipsis: false, title: '商品名称', dataIndex: 'name', key: 'name' },
  { ellipsis: false, title: '分类', key: 'type' },
  { ellipsis: false, title: '商品类型', dataIndex: 'className', key: 'className' },
  { ellipsis: false, title: '租售类型', key: 'category' },
  { ellipsis: false, title: '商品价格(元)', key: 'price' },
  { ellipsis: false, title: '起步数量', key: 'startNum', dataIndex: 'startNum' },
  { ellipsis: false, title: '租赁价格(元)', key: 'leasePrice' },
  { ellipsis: false, title: '商品介绍图', key: 'bigPicture' },
  { ellipsis: false, title: '是否上架', key: 'isUp' },
  { ellipsis: false, title: '单位', dataIndex: 'unit', key: 'unit' },
  { ellipsis: false, title: '规格', dataIndex: 'specs', key: 'specs' },
  { ellipsis: false, title: '操作', key: 'set', width: '160px', align: 'center' }
])

// 编辑数据
const edit = (item) => {
  emits('edit', item)
}

//删除数据
const del = (item) => {
  Modal.warning({
    title: '删除农资数据',
    content: h(
      'span',
      {},
      '是否删除农资数据：',
      h('span', { style: { color: '#09c690' } }, item.name)
    ),
    okText: '删除',
    okCancel: true,
    okButtonProps: { type: 'primary', danger: true },
    cancelButtonProps: { type: 'primary' },
    cancelText: '取消',
    onOk: async () => {
      await api.delNz({ id: item.id })
      message.success('删除成功')
      emits('reload')
      return Promise.resolve()
    },
    onCancel: () => {
      message.success('取消删除')
      return Promise.resolve()
    }
  })
}

provide('columns', columns.value)
</script>

<template>
  <MyTable :pagination="pagination" :scroll="{ y: 'calc(100vh - 383px)' }">
    <template #bodyCell="{ column, record }">
      <!-- <template v-if="column.key === 'smallPicture' && record.smallPicture">
        <ImageView :value="record.smallPicture" />
      </template> -->
      <template v-if="column.key === 'bigPicture' && record.bigPicture">
        <div v-for="(picture, index) in record.bigPicture.split(',')" :key="index">
          <ImageView :value="picture.trim()" />
        </div>
      </template>
      <template v-if="column.key === 'name'">
        {{ record.name }}
      </template>
      <template v-if="column.key === 'leasePrice'">
        {{ record.leasePrice ?? '--' }}
      </template>
      <template v-if="column.key === 'price'">
        {{ record.price ?? '--' }}
      </template>
      <template v-if="column.key === 'type'">
        {{ getConsants('sp_type', record.type) }}
      </template>
      <template v-if="column.key === 'category'">
        {{ getConsants('nz_type', record.category) }}
      </template>
      <template v-if="column.key === 'isUp'">
        {{ record.isUp === 1 ? '是' : '否' }}
      </template>
      <template v-if="column.key === 'set'">
        <AButton type="link" @click="edit(record)">编辑</AButton>
        <AButton type="link" class="com-delbtn" @click="del(record)">删除</AButton>
      </template>
    </template>
  </MyTable>
</template>